clear
set type double
set more 1

**********************************************
*firm charac by currency choice
*only manuf
*****************************************

use ../../rawdata/vat_declaration_monthly_updated, clear
	keep if year==2018
	collapse (sum) turnover, by(vat year)
save temp/turnover_2018, replace

use ../../rawdata/X_2018, clear 
	drop if land=="EU"
	collapse (sum) value, by(vat land month year flow)
		
	gen eurozone = inlist(land, "AT", "CY", "EE", "FI", "FR", "DE", "GR", "IE", "IT") 
	replace eurozone =1 if inlist(land, "LV", "LT", "LU", "MT", "NL", "PT", "SK", "SI", "ES")
	replace eurozone = 1 if inlist(land, "AD", "MC", "SM", "XK", "ME") 
	rename land cty_code
	gen value_eu=value*eurozone
	gen value_xeu=value*(1-eurozone)
	merge m:1 cty_code using ../data/usd_peg, keep(match master) nogen
	gen value_peg=value*(usd_peg==1)

	collapse (sum) value*, by(vat year)
	merge 1:1 vat year using temp/turnover_2018, keep(match master) nogen
	replace turnover=. if turnover<=0
		replace value_eu=0 if missing(value_eu)
		replace value_xeu=0 if missing(value_xeu)
		replace value_peg=0 if missing(value_peg)
		gen ez_exportsh = value_eu/value
		gen xeu_exportsh = value_xeu/value
		gen peg_exportsh = value_peg/value
		gen xvalue_sales=value/turnover
		rename value xvalue
		drop value*
		drop year
	
save temp/xvalue_2018, replace


use ../../rawdata/M_2018, clear
	collapse (sum) mvalue=value, by(vat)
save temp/mvalue_2018, replace	
	


use ../../rawdata/update_feb21/currency_extraEU_2017_2020, clear
	rename country cty_code
	merge m:1 vat cncode cty_code year month using ../data/xcurrencychoice_estdata, keepusing(Ix value mphi_xeu mphi_xeu_euro mphi_xeu_other mphi_xeu_dollar ms memp) nogen
	drop if missing(Ix)


drop date *I
	drop if missing(valueX)
    	drop if missing(currency)
	drop if cty_code=="EU"
	rename valueX value_curr

	merge m:1 vat using ../data/manuf_firm, keep(match) nogen
	merge m:1 cty_code using ../data/usd_peg, keep(match master) nogen
	merge m:1 cty_code using ../rawdata/iso_cty_and_currency, keep(match master) nogen
	replace cty_name="User" if cty_code=="QW"
	
*currency type
	drop if missing(currency)
	keep if year==2018
		
	gen type="EUR" if currency=="EUR"
	replace type="US" if currency=="USD"
	replace type="LC" if currency==currency_code & missing(type)
	replace type="NA" if missing(type)
	
	replace value=value/10^6
	gen value_EUR=value*(type=="EUR")
	gen value_US=value*(type=="US")
	gen value_LC=value*(type=="LC")
	gen value_NA=value*(type=="NA")
		
	collapse (sum) value*  (mean) mphi* ms* memp, by(vat)
	
*merge in other charach
	merge 1:1 vat using temp/xvalue_2018, keep(match master) nogen
	merge 1:1 vat using temp/mvalue_2018, keep(match master) nogen
		
	gen shEUR=value_EUR/value
	gen shUS=value_US/value
	gen shLC=value_LC/value
	gen shNA=value_NA/value
	
	egen max=rowmax(sh*)
	
	gen cat="EUR" if max==shEUR
	replace cat="US" if max==shUS & missing(cat)
	replace cat="LC" if max==shLC & missing(cat)
	replace cat="NA" if missing(cat)
	
	replace xvalue=xvalue/10^6
	replace turnover=turnover/10^6
	replace mvalue=mvalue/10^6
	
	estpost summarize memp turnover xvalue mvalue xvalue_sales xeu_exportsh ez_exportsh peg_exportsh ms mphi_xeu mphi_xeu_euro mphi_xeu_other mphi_xeu_dollar if cat=="EUR", detail
	
esttab using "../results/sumstats/TableA3.csv", cells("mean sd p5 p50 p95 count") title("EUR") noobs nomtitles nonum replace
eststo clear

	estpost summarize memp turnover xvalue mvalue xvalue_sales xeu_exportsh ez_exportsh peg_exportsh ms mphi_xeu mphi_xeu_euro mphi_xeu_other mphi_xeu_dollar if cat=="US", detail

esttab using "../results/sumstats/TableA3.csv", cells("mean sd p5 p50 p95 count") title("US") noobs nomtitles nonum append
eststo clear


	estpost summarize memp turnover xvalue mvalue xvalue_sales xeu_exportsh ez_exportsh peg_exportsh ms mphi_xeu mphi_xeu_euro mphi_xeu_other mphi_xeu_dollar if cat=="LC", detail

esttab using "../results/sumstats/TableA3.csv", cells("mean sd p5 p50 p95 count") title("LC") noobs nomtitles nonum append
eststo clear
	

